package distributedbank;

import java.io.IOException;

import distributedbank.fdservice.FailureDetectionServer;
import distributedbank.fdservice.FailureDetectionVoter;
import distributedbank.gui.BranchGui;
import distributedbank.sensor.Sensor;
import distributedbank.server.BankBranch;

public class Main {

	// if set to true, there is a 5-second delay before TransferDeposit messages
	// are sent
	public static boolean doDelayTransfers = false;

	public static boolean doDelaySnapshots = false;

	/**
	 * USAGE 1: java distributedbank.Main client 1
	 * 
	 * USAGE 2: java distributedbank.Main server 1
	 * 
	 * Where "1" should be replaced by the desired branchId for the process.
	 * Decides whether to run the BranchGui or the BankBranch main() method,
	 * depending on the first argument.
	 * 
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// if (args.length == 3 && args[2].equals("-delay")) {
		// Main.doDelayTransfers = true;
		// args = new String[] { args[0], args[1] };
		// } else if (args.length == 3 && args[2].equals("-delay-snapshots")) {
		// Main.doDelayTransfers = true;
		// Main.doDelaySnapshots = true;
		// args = new String[] { args[0], args[1] };
		// }

		// TODO: Modify Launcher file.
		// We should start 1 FailureDetectionVoter, 7 FailureDetectionServers,
		// 12 Sensors, 12 BankBranches, 4 BranchGUIs.

		if (args[0].equals("client")) {
			BranchGui.main(new String[] { args[1] });
		} else if (args[0].equals("server")) {
			BankBranch.main(new String[] { args[1], args[2] });
		} else if (args[0].equals("voter")) {
			FailureDetectionVoter.main(new String[0]);
		} else if (args[0].equals("fdserver")) {
			FailureDetectionServer.main(new String[] { args[1] });
		} else if (args[0].equals("sensor")) {
			Sensor.main(new String[] { args[1] });
		} else {
			System.out
					.println("USAGE: %program {fdserver|sensor|voter|client|server} BRANCH_NUMBER [REPLICA_NUMBER]");
			System.exit(1);
		}
	}
}
